// pages/A3/A3.js
const db = wx.cloud.database()
const cont = db.collection('Order')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: "待发货",
    Orders:[],
  },
  return:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  OrderDetails:function(e){
    wx.navigateTo({
      url: "/pages/ShopAdmin/OrderDetails/OrderDetails?OrderId="+e.target.dataset.id,
    })
  },
  // 点击tab切换
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
      this.getOrder(e.target.dataset.current)
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getOrder:function(state){
  var that=this
  that.setData({
    Orders:[]
  })
  wx.showLoading({
    title: '加载中',
  })
  wx.cloud.callFunction({
    name: "login"
  }).then(res => {
    console.log(res.result.openid);
    cont.where({
      state:state,
      SellOpenid:res.result.openid
    }).get()
      .then(res => {
        // res.data 包含该记录的数据
        console.log(res.data)
        for(let i=0; i < res.data.length;i++){
          var id = "Orders[" + i + "].id"
          var Consignee = "Orders[" + i + "].Consignee"
          var DeliveryTime = "Orders[" + i + "].DeliveryTime"
          var GoodsId = "Orders[" + i + "].GoodsId"
          var ReceivingTime = "Orders[" + i + "].ReceivingTime"
          var Remarks = "Orders[" + i + "].Remarks"
          var SellOpenid = "Orders[" + i + "].SellOpenid"
          var Telephone = "Orders[" + i + "].Telephone"
          var openid = "Orders[" + i + "].openid"
          var address = "Orders[" + i + "].address"
          var color = "Orders[" + i + "].color"
          var number = "Orders[" + i + "].number"
          var rmb = "Orders[" + i + "].rmb"
          var rmb2 = "Orders[" + i + "].rmb2"
          var size = "Orders[" + i + "].size"
          var state = "Orders[" + i + "].state"
          var GoodsTitle = "Orders[" + i + "].GoodsTitle"
          var SellNickName = "Orders[" + i + "].SellNickName"
          var SellAvatarUrl = "Orders[" + i + "].SellAvatarUrl"
          var GoodsImg = "Orders[" + i + "].GoodsImg"
          var express = "Orders[" + i + "].express"
          that.setData({
            [id] :res.data[i]._id,
            [Consignee]:res.data[i].Consignee,
            [DeliveryTime]:res.data[i].DeliveryTime,
            [GoodsId]:res.data[i].GoodsId,
            [ReceivingTime]:res.data[i].ReceivingTime,
            [Remarks]:res.data[i].Remarks,
            [SellOpenid]:res.data[i].SellOpenid,
            [Telephone]:res.data[i].Telephone,
            [openid]:res.data[i]._openid,
            [address]:res.data[i].address,
            [color]:res.data[i].color,
            [number]:res.data[i].number,
            [rmb]:res.data[i].rmb.toFixed(2),
            [rmb2]:(res.data[i].rmb*res.data[i].number+res.data[i].express).toFixed(2),
            [size]:res.data[i].size,
            [state]:res.data[i].state,
            [GoodsTitle]:res.data[i].GoodsTitle,
            [SellNickName]:res.data[i].SellNickName,
            [SellAvatarUrl]:res.data[i].SellAvatarUrl,
            [GoodsImg]:res.data[i].GoodsImg,
            [express]:res.data[i].express,
          })

        }
       console.log( this.data.Orders)
       wx.hideLoading();
      })
      .catch(err => {
        console.error(err)
        wx.hideLoading();
    })

  }).catch(err => {
    console.log(err);
    wx.hideLoading();
  })
},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrder(this.data.currentTab)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})